﻿using System;
using System . Collections . Generic;
using System . Collections . ObjectModel;
using System . Linq;
using System . Text;
using System . IO;
using System . Threading;
using System . Threading . Tasks;
using System . Xml . Linq;

namespace WenceyWang . RichMan4L
{
	public class GameSaying
	{
		public string Context { get; private set; } = null;

		public string People { get; private set; } = null;

		public string Book { get; private set; } = null;

		public string Author { get; private set; } = null;

		public string Song { get; private set; } = null;

		public GameSaying ( XElement element )
		{
			if ( element == null )
			{
				throw new ArgumentNullException ( nameof ( element ) );
			}
			Context = element . Attribute ( nameof ( Context ) )?.Value;
			People = element . Attribute ( nameof ( People ) )?.Value;
			Book = element . Attribute ( nameof ( Book ) )?.Value;
			Author = element . Attribute ( nameof ( Author ) )?.Value;
			Song = element . Attribute ( nameof ( Song ) )?.Value;
		}

		static List<GameSaying> sayings = null;

		public static GameSaying GetSaying ( )
		{
			if ( sayings == null )
			{
				LoadSayings ( ) . Wait ( );
			}
			Random ran = new Random ( );
			return sayings [ ran . Next ( 0 , sayings . Count ) ];
		}

		public static Task LoadSayings ( )
		{
			Task loadTask = new Task ( ( ) =>
			{
				sayings = new List<GameSaying> ( );

				var doc = ResourceHelper . LoadXmlDocument ( @"SayingResources.xml" );

				foreach ( var item in doc . Root . Elements ( ) )
				{
					sayings . Add ( new GameSaying ( item ) );
				}


			} );

			loadTask . Start ( );

			return loadTask;
		}
	}
}
